﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Deanery.Controller.Directories
{
    class SexController
    {
        private readonly deaneryEntities _db = new deaneryEntities();

        /// <summary>
        /// This method transfers columns name in ukrainian for view table Sex
        /// </summary>
        /// <returns>Array of string with columns names</returns>
        public Array ColumnName()
        {
            string[] columnName = { "ID", "Стать" };
            return columnName;
        }

        /// <summary>
        /// This method transfers columns data type in table Sex
        /// </summary>
        /// <returns>Array of string with columns data type</returns>
        public Array ColumnValueType()
        {
            string[] columnDataType = { "guid", "char" };
            return columnDataType;
        }

        /// <summary>
        /// This method select all reccords from table Sex
        /// </summary>
        /// <returns>Set of IQueryable</returns>
        public IQueryable GetAll()
        {
            var list = _db.Sex.Select(f => f);
            return list;
        }

        /// <summary>
        /// This method performs INSERT one record to DB in table Sex
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="reccord">ArrayList(one item - one cell value) with data to insert</param>
        public void Insert(ArrayList reccord)
        {
            var obj = new Sex
            {
                id_sex = GenerateGuid(),
                ch_sex_name = reccord[0].ToString()
            };
            _db.Sex.AddObject(obj);
            _db.SaveChanges();
        }

        /// <summary>
        /// This method performs UPDATE one record to DB in table Sex
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="updateGuid">guid value to search reccord in table</param>
        /// <param name="reccord">ArrayList(one item - one cell value) with data to update</param>
        public void Update(Guid updateGuid, ArrayList reccord)
        {
            var obj = (_db.Sex.Where(t => t.id_sex == updateGuid)).Single();
            obj.ch_sex_name = reccord[0].ToString();
            _db.SaveChanges();
        }

        /// <summary>
        /// This method generates new guid
        /// </summary>
        /// <returns>New guid</returns>
        public Guid GenerateGuid()
        {
            var guid = Guid.NewGuid();
            return guid;
        }

        /// <summary>
        /// This method find reccord guid by name in table Sex
        /// </summary>
        /// <param name="name">string value to search reccord in table</param>
        /// <returns>guid value of finded reccord</returns>
        public Guid FindGuidByName(string name)
        {
            var obj = (_db.Sex.Where(t => t.ch_sex_name == name.Trim(' ')).Select(t => t.id_sex)).SingleOrDefault();
            return obj;
        }
    }
}
